
import sqlalchemy
from fastapi import Request
from pydantic import BaseModel, Field

from .api_model.response_model import ApiResponse
from .teacher import check_user_is_teacher_class, check_user_has_class_position


async def get_announcement_list_api(request: Request, class_name: str, engine):
    conn = engine.connect()
    try:
        result = conn.execute(sqlalchemy.text("""
                        SELECT announcement.announcement_name, announcement.publish_time, announcement.content,
                        announcement.publisher_uid, user_info.username 
                        FROM announcement, user_info
                        WHERE announcement.class_name=:class_name AND user_info.uid = announcement.publisher_uid
                    """), {
                "class_name": class_name
        })
        announcement_list = result.fetchall()
    except Exception as e:
        print(e)
        return ApiResponse(code=500, msg="查询班级列表信息发生错误")
    finally:
        conn.close()

    response = ApiResponse(code=200,
                           data={
                           },
                           msg="查询成功")
    response.data = {
        "class_name": class_name,
        "announcement_list": [
            {
                "announcement_name": announcement[0],
                "publish_time": announcement[1],
                "content": announcement[2],
                "publisher_uid": announcement[3],
                "username": announcement[4]
            } for announcement in announcement_list
        ]
    }
    return response


class SendAnnouncementModel(BaseModel):
    announcement_name: str = Field(..., min_length=1)
    class_name: str = Field(..., min_length=1, max_length=50, description="班级")
    content: str = Field(..., min_length=1)


async def send_announcement_api(request: Request, data: SendAnnouncementModel, engine):
    token_uid = request.state.user.get("uid")
    teacher_auth = check_user_is_teacher_class(token_uid, data.class_name, engine)
    stu_position_auth = check_user_has_class_position(token_uid, engine)
    if not teacher_auth and not stu_position_auth:
        print(teacher_auth, stu_position_auth)
        return ApiResponse(code=400, msg=f'{token_uid} 无 {data.class_name} 公告权限')

    conn = engine.connect()
    try:
        conn.execute(sqlalchemy.text("""
                        INSERT INTO announcement (announcement_name, publish_time, content, class_name, publisher_uid) 
                        VALUES (:announcement_name, NOW(), :content, :class_name, :publisher_uid)
                    """), {
                "announcement_name": data.announcement_name,
                "class_name": data.class_name,
                "content": data.content,
                "publisher_uid": token_uid,
        })
        conn.commit()
    except Exception as e:
        print(e)
        return ApiResponse(code=500, msg="发送失败")
    finally:
        conn.close()

    response = ApiResponse(code=200,
                           data={
                           },
                           msg="发送成功")
    return response
